//
//  NativeViewController.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "NativeViewController.h"
#import <CJMobileAd/CJMobileAd.h>
#import <AMPSAdSDK/AMPSAdSDK.h>

@interface NativeViewController ()<AMPSNativeExpressManagerDelegate, AMPSNativeExpressViewDelegate>

@property (nonatomic, strong) AMPSNativeExpressManager *nativeExpressManager;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray <UIView *>*nativeAdViews;

@end

@implementation NativeViewController

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)setupUI {
    self.title = @"Native";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;

}

- (void)loadAdData {
    [self.nativeExpressManager loadNativeExpressManager];
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak NativeViewController *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

- (AMPSNativeExpressManager *)nativeExpressManager {
    if (!_nativeExpressManager) {
        AMPSAdConfiguration *cfg = [[AMPSAdConfiguration alloc]init];
        cfg.adSize = CGSizeMake(self.view.frame.size.width, 0);
        cfg.viewController = self;
        _nativeExpressManager = [[AMPSNativeExpressManager alloc]initWithSpaceId:@"124546" adConfiguration:cfg];
        _nativeExpressManager.delegate = self;
    }
    return _nativeExpressManager;
}

- (NSMutableArray<UIView *> *)nativeAdViews {
    if (!_nativeAdViews) {
        _nativeAdViews = [NSMutableArray new];
    }
    return _nativeAdViews;
}

#pragma mark <AMPSNativeExpressManagerDelegate, AMPSNativeExpressViewDelegate>
- (void)ampsNativeAdLoadSuccess:(AMPSNativeExpressManager *)nativeAd {
    NSLog(@"事件:%s", __FUNCTION__);
    for (int i = 0; i < nativeAd.viewsArray.count; i++) {
        AMPSNativeExpressView *expressView = [nativeAd.viewsArray objectAtIndex:i];
        expressView.delegate = self;
        expressView.viewController = self;
        expressView.tag = i;
        [expressView render];
    }
}

- (void)ampsNativeAdLoadFail:(AMPSNativeExpressManager *)nativeAd
                       error:(NSError *_Nullable)error {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)ampsNativeAdRenderSuccess:(AMPSNativeExpressView *)nativeView {
//    [self.adArray adScopeSafeAddObject:nativeView];
    NSLog(@"事件:%s", __FUNCTION__);
    CGRect frame = nativeView.frame;
    frame.size.height = 300;
    nativeView.frame = frame;
    [self.view addSubview:nativeView];
    
    
    NSLog(@"kkkk----%@", nativeView);
    
}

- (void)ampsNativeAdRenderFail:(AMPSNativeExpressView *)nativeView
                         error:(NSError *_Nullable)error {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)ampsNativeAdExposured:(AMPSNativeExpressView *)nativeView {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)ampsNativeAdDidClick:(AMPSNativeExpressView *)nativeView {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)ampsNativeAdDidClose:(AMPSNativeExpressView *)nativeView {
    NSLog(@"事件:%s", __FUNCTION__);
    [nativeView removeFromSuperview];
    [nativeView removeNativeAd];
}

@end
